#include "heap.h"
#include <time.h>
void PrintTopK(int* a, int n, int k)
{
	HN hp;
	HeapInit(&hp);
	for (int i = 0; i < n; i++)
	{
		HeapPush(&hp, a[i]);
		if (HeapSize(&hp) > k)
		{
			HeapPop(&hp);
		}
	}

	// 弹出元素按顺序打印
	while (!HeapEmpty(&hp))
	{
		printf("%d ", HeapTop(&hp)); // 输出堆顶元素
		HeapPop(&hp);            // 弹出堆顶元素
	}
	printf("\n");

	HeapDestroy(&hp);
}


int main()
{
	int n = 10000;
	int* a = (int*)malloc(sizeof(int) * n);
	if (a == NULL) 
	{
		fprintf(stderr, "Memory allocation failed\n");
		exit(EXIT_FAILURE); // 退出程序以防止后续对 NULL 的解引用
	}

	srand(time(0));
	for (size_t i = 0; i < n; ++i)
	{
		a[i] = rand() % 1000000;//生成一百万以内的随机数
	}
	a[5] = 1000000 + 1;
	a[1231] = 1000000 + 2;
	a[531] = 1000000 + 3;
	a[5121] = 1000000 + 4;
	a[115] = 1000000 + 5;
	a[2335] = 1000000 + 6;
	a[9999] = 1000000 + 7;
	a[76] = 1000000 + 8;
	a[423] = 1000000 + 9;
	a[3144] = 1000000 + 10;
	PrintTopK(a, n, 10);

}